package src;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import maquinavirtual.MaquinaVirtual;
import resources.Compilador;
import semantic.CodInstrEnum;


/**
 * 
 * contem as ActionListener da classe Window
 *  *  
 * @author eklauberg
 *
 */
public class WindowActions implements ActionListener, DocumentListener {

	private Window window;
	
	public WindowActions(Window window) {
		this.window = window;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == window.getBtnNovo()
				|| e.getSource() == window.getMntmNovo()) {
			/* NOVO */
			window.getFerramentas().novo();
		} else if (e.getSource() == window.getBtnAbrir() || e.getSource() == window.getMntmAbrir()) {
			/* ABRIR  */
			window.getFerramentas().abrir();
		} else if (e.getSource() == window.getBtnSalvar() || e.getSource() == window.getMntmSalvar()) {
			/* SALVAR */
			window.getFerramentas().salvar();
		} else if (e.getSource() == window.getMntmSalvarComo()) {
			/* SALVAR COMO  */
			window.getFerramentas().salvarComo();
		} else if (e.getSource() == window.getBtnRecortar()	|| e.getSource() == window.getMntmRecortar()) {
			/* RECORTAR */
			window.getFerramentas().recortar();
		} else if (e.getSource() == window.getBtnCopiar() || e.getSource() == window.getMntmCopiar()) {
			/* COPIAR */
			window.getFerramentas().copiar();
		} else if (e.getSource() == window.getBtnColar() || e.getSource() == window.getMntmColar()) {
			/* COLAR */
			window.getFerramentas().colar();
		} else if (e.getSource() == window.getBtnX()) {
			/* X */
			System.out.println("X - Não implementado...");
		} else if (e.getSource() == window.getBtnDesfazer()) {
			/* DESFAZER */
			System.out.println("Desfazer - Não implementado...");
		} else if (e.getSource() == window.getMntmSair()) {
			/* SAIR */
			window.getFerramentas().sair();
		} else if (e.getSource() == window.getMntmExecutar()) {
			/* EXECUTAR */
//			System.out.println("Executar - Não implementado...");
//			MaquinaVirtual maquinaVirtual = new MaquinaVirtual();
//			MaquinaVirtual.areaInstrucoes.put(1, new Object[]{CodInstrEnum.ALI,2});
//			MaquinaVirtual.areaInstrucoes.put(2, new Object[]{CodInstrEnum.REA,1});
//			MaquinaVirtual.areaInstrucoes.put(3, new Object[]{CodInstrEnum.STR,1});
//			MaquinaVirtual.areaInstrucoes.put(4, new Object[]{CodInstrEnum.REA,1});
//			MaquinaVirtual.areaInstrucoes.put(5, new Object[]{CodInstrEnum.STR,2});
//			MaquinaVirtual.areaInstrucoes.put(6, new Object[]{CodInstrEnum.LDV,1});
//			MaquinaVirtual.areaInstrucoes.put(7, new Object[]{CodInstrEnum.LDV,2});
//			MaquinaVirtual.areaInstrucoes.put(8, new Object[]{CodInstrEnum.BGR,0});
//			MaquinaVirtual.areaInstrucoes.put(9, new Object[]{CodInstrEnum.JMF,13});
//			MaquinaVirtual.areaInstrucoes.put(10, new Object[]{CodInstrEnum.LDV,1});
//			MaquinaVirtual.areaInstrucoes.put(11, new Object[]{CodInstrEnum.WRT,0});
//			MaquinaVirtual.areaInstrucoes.put(12, new Object[]{CodInstrEnum.JMP,15});
//			MaquinaVirtual.areaInstrucoes.put(13, new Object[]{CodInstrEnum.LDV,2});
//			MaquinaVirtual.areaInstrucoes.put(14, new Object[]{CodInstrEnum.WRT,0});
//			MaquinaVirtual.areaInstrucoes.put(15, new Object[]{CodInstrEnum.STP,0});
			InputStream stream = new ByteArrayInputStream(window.getTxtCodigo()
					.getText().toLowerCase().getBytes());
			Compilador compilador = new Compilador(stream);
			compilador.compilar();
			
			MaquinaVirtual maquinaVirtual = new MaquinaVirtual();
			MaquinaVirtual.areaInstrucoes = compilador.getSemantic().getAreaInstrucoes();
			maquinaVirtual.executar();
			
		} else if (e.getSource() == window.getMntmCompilar()) {
			/* COMPILAR */
			window.getTxtConsole().setText("");
			InputStream stream = new ByteArrayInputStream(window.getTxtCodigo()
					.getText().toLowerCase().getBytes());
			Compilador compilador = new Compilador(stream);
			compilador.compilar();
			
			this.createJtableInstrucoes(compilador.getSemantic().getAreaInstrucoes());			
		}
	}

	private void createJtableInstrucoes(Map<Integer, Object[]> areaInstrucoes) {
		
		JFrame frame = new JFrame();
	    int numSize = areaInstrucoes.size();
	    
	    Object rowData[][] = new Object[numSize][3];
	    
	    for (int i = 1; i <= numSize; i++) {
	    	Object[] object = areaInstrucoes.get(i);
	    	rowData[i-1][0] = i;
	    	rowData[i-1][1] = ((CodInstrEnum)object[0]).name();
	    	rowData[i-1][2] = object[1];
		}

	    Object columnNames[] = { "n°", "código", "par."};
	    JTable table = new JTable(rowData, columnNames);

	    JScrollPane scrollPane = new JScrollPane(table);
	    frame.add(scrollPane, BorderLayout.CENTER);
	    frame.setSize(300, 300);
	    frame.setVisible(true);
		
		
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		this.window.getFerramentas().getDocument().setModificado(true);
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		this.window.getFerramentas().getDocument().setModificado(true);
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		this.window.getFerramentas().getDocument().setModificado(true);
	}

}
